package com.youdianstar.app.adapter

import android.content.Context
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.common.Language
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.util.DateUtil
import com.youdianstar.app.common.util.LanguageConfig
import com.youdianstar.app.common.util.MathUtil
import com.youdianstar.app.common.util.StrUtil
import com.youdianstar.app.R
import com.youdianstar.app.databinding.LayoutItemCouponHomeBinding
import com.youdianstar.app.entity.CouponRes
import com.youdianstar.app.util.DataAppManager
import com.youdianstar.app.common.ex.parseTx


/**
 * File: CouponHomeAdapter
 * Author: 82149 Create: 2023/7/27 11:51
 * Changes (from 2023/7/27)
 * --------------------------------------------------
 * description: 优惠券主页 列表
 * ---------------------------------------------------
 *
 */
class CouponHomeAdapter(val mContext: Context, val dataList: List<CouponRes>): RecyclerView.Adapter<CouponHomeAdapter.ListHolder>() {

    class ListHolder(val binding : LayoutItemCouponHomeBinding) : RecyclerView.ViewHolder(binding.root)

    var languageNow :String = ""
    init {
        languageNow = LanguageConfig.instance.getLanguageCode()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListHolder {
        return ListHolder(LayoutItemCouponHomeBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: ListHolder, position: Int) {
        dataList[position].let { item ->
            if (languageNow == Language.ES){
                holder.binding.tvCouponValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18F)
            }else{
                holder.binding.tvCouponValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22F)
            }
            holder.binding.tvCouponTime.text = mContext.getString(R.string.order_expiryDate)+": "+ DateUtil.getOrderDate(item.endTime?:0L)
            //抵扣类型:1-金额 2-时间 3-折扣
            val symbolStr = DataAppManager.instance.symbolTx()
            var deductName = "--"
            var deductTx = ""
            item.deduction?.let { bean->
                //val bean = GsonUtil.jsonToBean(json, CouponDeduct::class.java)
                when(bean.type){
                    1-> {
                        deductName = mContext.getString(R.string.coupon_freeUsage)
                        //1-分钟 2-小时
                        deductTx = when(bean.type1FreeDurationUnit){
                            1->{
                                (if (bean.type1FreeDurationVal>0) bean.type1FreeDurationVal.toString() else "0")+" "+
                                        mContext.getString(R.string.coupon_min)
                            }

                            2->{
                                (if (bean.type1FreeDurationVal>0) bean.type1FreeDurationVal.toString() else "0")+" "+
                                        mContext.getString(R.string.coupon_hour)
                            }

                            else->{
                                (if (bean.type1FreeDurationVal>0) bean.type1FreeDurationVal.toString() else "0")+" "+
                                        mContext.getString(R.string.coupon_day)
                            }
                        }
                    }
                    2-> {
                        deductName = mContext.getString(R.string.coupon_freeRental)
                        deductTx = symbolStr+MathUtil.round(bean.type2DiscountAmount?:0.0)
                    }
                    3->{
                        deductName = mContext.getString(R.string.coupon_rentalDiscount)
                        deductTx = String.format(mContext.getString(R.string.coupon_off),bean.type3DiscountNum()+"%")
                    }
                }
            }
            holder.binding.tvCouponName.text = deductName
            holder.binding.tvCouponValue.text = deductTx
            //状态: 1-未使用 2-已使用 3-已过期
            when(item.state){
                2->{
                    holder.binding.tvCouponName.alpha = 0.2F
                    holder.binding.tvCouponTime.alpha = 0.2F
                    holder.binding.tvCouponValue.alpha = 0.2F
                    holder.binding.ivEpDex.alpha = 0.2F
                    holder.binding.txEpDes.alpha = 0.2F
                    holder.binding.clCouponPart2.alpha = 0.2F
                    holder.binding.ivStateTag.visibility = View.VISIBLE
                    holder.binding.ivStateTag.setText(mContext.getString(R.string.coupon_used))
                }
                3->{
                    holder.binding.tvCouponName.alpha = 0.2F
                    holder.binding.tvCouponTime.alpha = 0.2F
                    holder.binding.tvCouponValue.alpha = 0.2F
                    holder.binding.ivEpDex.alpha = 0.2F
                    holder.binding.txEpDes.alpha = 0.2F
                    holder.binding.clCouponPart2.alpha = 0.2F
                    holder.binding.ivStateTag.visibility = View.VISIBLE
                    holder.binding.ivStateTag.setText(mContext.getString(R.string.coupon_expired))
                }
                else ->{
                    holder.binding.tvCouponName.alpha = 1F
                    holder.binding.tvCouponTime.alpha = 1F
                    holder.binding.tvCouponValue.alpha = 1F
                    holder.binding.ivEpDex.alpha = 1F
                    holder.binding.txEpDes.alpha = 1F
                    holder.binding.clCouponPart2.alpha = 1F
                    holder.binding.ivStateTag.visibility = View.INVISIBLE
                }
            }

            //1. 使用说明
            var desTx = "--"
            item.description?.let { des ->
                desTx = when(languageNow){
                    Language.ES -> des.es.parseTx()
                    Language.ZH -> des.zh.parseTx()
                    else -> des.en.parseTx()
                }
            }
            holder.binding.tvDesContent.text = desTx
            //2. 可用商家;
            if (StrUtil.isEmpty(item.merchantNameList)){
                holder.binding.tvUseContent.text = mContext.getString(R.string.coupon_allStationUse)
            }else{
                holder.binding.tvUseContent.text = item.merchantNameList
            }
            holder.binding.ivEpDex.rotation = if (item.isExp) 180F else 0F
            holder.binding.clCouponPart2.visibility = if (item.isExp) View.VISIBLE else View.GONE
        }

        holder.binding.ivEpDex.setThrottleListener {
            onItemViewExpAction(holder.bindingAdapterPosition)
        }
        holder.binding.txEpDes.setThrottleListener {
            onItemViewExpAction(holder.bindingAdapterPosition)
        }
    }

    /**更新布局展开状态*/
    private fun updateItemViewExp(position: Int, expAction:Boolean){
        dataList[position].isExp = expAction
        notifyItemChanged(position)
//        notifyDataSetChanged()
    }

    private fun onItemViewExpAction(position: Int){
        dataList[position].let { info ->
            val isExpNow = info.isExp
            updateItemViewExp(position, !isExpNow)
        }
    }
}